package domain.game;

import sounds.Sounds;
import images.Images;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.appearances.Animation;
import com.uqbar.vainilla.events.constants.Key;

import domain.components.IndicadorActualWorm;
import domain.weapon.Bazooka;
import domain.weapon.Dynamite;
import domain.weapon.Gun;
import domain.weapon.Weapon;
import domain.worm.Worm;

public class PlayingState extends GameState {

	Bazooka bazooka;
	Gun gun;
	Dynamite dynamite;
	
	public PlayingState(KeysHandler kh) {
		super(kh);
		this.bazooka = new Bazooka();
		this.gun = new Gun();
		this.dynamite = new Dynamite();
	}
	

	public void update (DeltaState delta){
		if (delta.isKeyBeingHold(Key.RIGHT) | delta.isKeyPressed(Key.RIGHT)){
			this.currentWorm().walk(delta, 1);
		}
		if (delta.isKeyBeingHold(Key.LEFT) | delta.isKeyPressed(Key.LEFT)){
			this.currentWorm().walk(delta, -1);
		}

		if (delta.isKeyBeingHold(Key.ENTER) | delta.isKeyPressed(Key.ENTER)){
			this.currentWorm().jump();
		}
		if (delta.isKeyBeingHold(Key.UP) | delta.isKeyPressed(Key.UP) | delta.isKeyBeingHold(Key.DOWN) | delta.isKeyPressed(Key.DOWN)){
			this.currentWorm().aim();
		}
		if (delta.isKeyPressed(Key.T)){
			this.currentWorm().setStill();
			this.getKeysHandler().getScene().changeTurn();
		}
		if (delta.isKeyPressed(Key.E)){
			for(Team t:this.getKeysHandler().getScene().getBattleManager().getTeams()){
				for(Worm w:t.getWorms()){
					w.showTeam();
				}
			}
		}
		
		if (delta.isKeyPressed(Key.A)){
			this.currentWorm().showIndicador();
		}
		
		//Seleccionar Bazooka
		if(delta.isKeyPressed(Key.ONE)){
			this.currentWorm().setWeapon(bazooka);
			this.getKeysHandler().getScene().changeWeapon("bazooka");
			if(this.currentWorm().getDir() == 1)
				this.currentWorm().setAppearance(this.bazooka.getAppWeaponRight());
			else
				this.currentWorm().setAppearance(this.bazooka.getAppWeaponLeft());
			(Sounds.sounds.get("reloadBazooka")).play();

		}

		//Seleccionar Escopeta
		if(delta.isKeyPressed(Key.TWO)){
			this.currentWorm().setWeapon(gun);
			this.getKeysHandler().getScene().changeWeapon("gun");
			if(this.currentWorm().getDir() == 1)
				this.currentWorm().setAppearance(this.gun.getAppWeaponRight());
			else
				this.currentWorm().setAppearance(this.gun.getAppWeaponLeft());
			(Sounds.sounds.get("reloadGun")).play();
		}

		//Seleccionar Dinamita
		if(delta.isKeyPressed(Key.THREE)){
			this.currentWorm().setWeapon(dynamite);
			this.getKeysHandler().getScene().changeWeapon("dynamite");
			if(this.currentWorm().getDir() == 1)
				this.currentWorm().setAppearance(this.dynamite.getAppWeaponRight());
			else
				this.currentWorm().setAppearance(this.dynamite.getAppWeaponLeft());
			(Sounds.sounds.get("reloadDynamite")).play();
		}

	}
	
	public Worm currentWorm(){
		return this.getKeysHandler().getScene().currentWorm();
	}
}
